<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\circle;

use app\api\model\dao\Circle;
use app\api\model\dao\CircleCategory;
use app\api\model\dao\Note;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserFollow;
use app\api\model\data\circle\CircleData;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Log;
use think\Validate;

class GetCircleUser extends PageBase
{
    private $objUserFollow;
    private $objUserAccount;
    private $objNote;

    public function __construct()
    {
        parent::__construct();

        $this->objUserFollow = new UserFollow();
        $this->objUserAccount = new UserAccount();
        $this->objNote = new Note();

    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'cir_id' => 'require|number',
        ];
        $msg = [
            'cir_id.require' => '请选择圈子',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $arrCond = [
            'follow_id' => ['=', $param['cir_id']],
            'follow_type' => ['=', Flag::CONST_FOLLOW_CIRCLE],
        ];
        $arrField = ['userid'];

        if (isset($param['new'])) {
            $yestoday = date('Ymd', time() - 86400);
            $arrCond['date'] = ['>=', $yestoday];
        }

        $user_follow = $this->objUserFollow->getFollow($arrCond, $arrField);
        if (false === $user_follow) {
            Log::error(__METHOD__ . ' get user_follow fail.[conds: ' . json_encode($arrCond) . ']');
            return $this->getRes(Error::ERR_SYS);
        }

        $this->data['list'] = [];

        $userid = [];
        $userid2UserAccount = [];
        if (!empty($user_follow)) {
            foreach ($user_follow as $val) {
                $userid[] = $val['userid'];
            }
        }

        $userid2NoteCount = [];
        $userid2NoteCountDay = [];
        if (!empty($userid)) {
            $userid = array_unique($userid);
            $user_account = $this->objUserAccount->getUserAccountByUserids($userid);
            if (empty($user_account)) {
                Log::error(__METHOD__ . ' get user_account fail.[userid: ' . json_encode($userid) . ']');
                return $this->getRes(Error::ERR_SYS);
            }
            foreach ($user_account as $val) {
                $userid2UserAccount[$val['id']] = $val;
            }
            $note_count = $this->objNote->getNoteCountByUserids($userid);
            if (false === $note_count) {
                Log::error(__METHOD__ . ' get note count fail.[userid: ' . json_encode($userid) . ']');
                return $this->getRes(Error::ERR_SYS);
            }
            foreach ($note_count as $val) {
                $userid2NoteCount[$val['creator']] = $val['note_cnt'];
            }
            $note_count = $this->objNote->getNoteCountByUserids($userid, ['date = ' => date('Ymd')]);
            if (false === $note_count) {
                Log::error(__METHOD__ . ' get note count fail.[userid: ' . json_encode($userid) . ']');
                return $this->getRes(Error::ERR_SYS);
            }
            foreach ($note_count as $val) {
                $userid2NoteCountDay[$val['creator']] = $val['note_cnt'];
            }
        }

        foreach ($userid2UserAccount as $user) {
            $data = [
                'userid' => $user['id'],
                'nick_name' => $user['nick_name'],
                'photo' => $user['avatar'],
                'sex' => $user['sex'],
                'level' => $user['level'],
                'note_count' => 0,
                'note_count_day' => 0,
            ];
            if (!empty($userid2NoteCount[$user['id']])) {
                $data['note_count'] = $userid2NoteCount[$user['id']];
            }
            if (!empty($userid2NoteCountDay[$user['id']])) {
                $data['note_count_day'] = $userid2NoteCount[$user['id']];
            }
            $this->data['list'][] = $data;
        }

        return $this->getRes();
    }
}
